﻿package andy.demo.mutiScreen;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import andy.demo.R;

/**
 * 测试 1：同样大小的屏幕  同一个分辨率 px ，不同的density (不同的dp) 
 * @author BoyceAndy
 *
 */

public class MutiScreen extends Activity {

	private TextView title;
	private TextView content;
	private TextView white;
	private TextView screen_info;
	private TextView tv_iv_1_info;
	private TextView tv_iv_2_info;
	private TextView tv_iv_3_info;
	private TextView tv_iv_4_info;
	private TextView tv_iv_5_info;
	
	private EditText number;
	private EditText pswd;
	
	private ImageView iv_1;
	private ImageView iv_2;
	private ImageView iv_3;
	private ImageView iv_4;
	private ImageView iv_5;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.muti_screen_px_dip_sp);
		
	
		screen_info = (TextView)this.findViewById(R.id.screen_info);
		tv_iv_1_info = (TextView)this.findViewById(R.id.tv_iv_1_info);
		tv_iv_2_info = (TextView)this.findViewById(R.id.tv_iv_2_info);
		tv_iv_3_info = (TextView)this.findViewById(R.id.tv_iv_3_info);
		tv_iv_4_info = (TextView)this.findViewById(R.id.tv_iv_4_info);
		tv_iv_5_info = (TextView)this.findViewById(R.id.tv_iv_5_info);
		iv_1 = (ImageView)this.findViewById(R.id.iv_1);
		iv_2 = (ImageView)this.findViewById(R.id.iv_2);
		iv_3 = (ImageView)this.findViewById(R.id.iv_3);
		iv_4 = (ImageView)this.findViewById(R.id.iv_4);
		iv_5 = (ImageView)this.findViewById(R.id.iv_5);
		
		// 计算状态栏高度 和 标题栏 高度 ***************************
		Rect frame = new Rect();
		getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		//状态栏的高度     frame.top 是指不包含状态栏的view的起始点
		int statusBarHeight = frame.top;
		System.out.println("top=" + frame.top + "bottom =" + frame.bottom + "left=" + frame.left + "right =" + frame.right + " statusBarHeight=" + statusBarHeight);
		
		//etWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏和任务栏的部分，然后就可以知道标题栏的高度了。
		int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
		//statusBarHeight是上面所求的状态栏的高度
		int titleBarHeight = contentTop - statusBarHeight;
		System.out.println("contentTop =" + contentTop +" title height =" + titleBarHeight);
		
		// 计算状态栏高度 和 标题栏 高度 ***************************
		System.out.println(getWindowInfo());
		System.out.println(getWindowInfo2());
		System.out.println(dip2px(this,160));
		System.out.println(px2dip(this,dip2px(this,50)));
		
		System.out.println(px2dip(this,50));
		System.out.println(dip2px(this,px2dip(this,50)));
		
		screen_info.setText(getWindowInfo2());
		System.out.println("screen_info WH=" + getViewWHInfo(screen_info));
		
		dip2px_2(this,160);
	}
	
	public static int dip2px(Context context, float dipValue){
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int)(dipValue * scale + 0.5f);
	}
	
	public  int dip2px_2(Context context, float dipValue){
		DisplayMetrics dm = new DisplayMetrics(); 
		getWindowManager().getDefaultDisplay().getMetrics(dm); 
		float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue , dm);
		System.out.println("dip2px_2 : px=" + px);
		return (int)px;
	}

	public static int px2dip(Context context, float pxValue){
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int)(pxValue / scale + 0.5f);
	}
	
	/**
	 * 通过窗口管理器 WindowManager对象 
	 * @return
	 */
	public String getWindowInfo() {
		WindowManager manage = this.getWindowManager(); 
		Display display = manage.getDefaultDisplay(); 
		int screenWidth = display.getWidth(); 
		int screenHeigth = display.getHeight(); 
		float density = display.getPixelFormat();
		return "screenWidth = " + screenWidth + "  screenHeight=" +  screenHeigth ;

	}
	
	/**
	 * 到DisplayMetrics对象，在将屏幕信息存储在DisplayMetrics对象中 
	 * @return
	 */
	public String getWindowInfo2() {
		DisplayMetrics dm = new DisplayMetrics(); 
		getWindowManager().getDefaultDisplay().getMetrics(dm); 
		
		/**
		 * 注意： 此处DisplayMetrics 不要使用context.getApplicationContext().getResources().getDisplayMetrics();
		 * 我用此方法在nexus one 中测试的时候得到的 density 的值是1.0  ，以至于我得不到正确的480 * 800 的像素。
		 */
//		dm = getApplication().getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels; 
		int screenHeigth = dm.heightPixels; 
		float density = dm.density;
		float densityDPI = dm.densityDpi;
		return "screenWidth = " + screenWidth + "  screenHeight=" +  screenHeigth + "  density=" + density + "\n" +
				"densityDPI=" + densityDPI + "DisplayMetrics=" + dm;
		
	}
	
	
	public String getViewWHInfo(View view) {
		
		int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int width =view.getMeasuredWidth();
        int height =view.getMeasuredHeight();
        
        return "{width=" + width + ", height=" + height + "}";

	} 
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		
		System.out.println("onWindowFocusChanged screen_info WH=" + getViewWHInfo(screen_info));
		System.out.println("onWindowFocusChanged screen_info W=" + screen_info.getWidth());
		System.out.println("onWindowFocusChanged screen_info H=" +screen_info.getHeight());
		System.out.println("onWindowFocusChanged iv_1 W=" +iv_1.getWidth());
		System.out.println("onWindowFocusChanged iv_1 H=" +iv_1.getHeight());
		
		tv_iv_1_info.setText("iv_1 W=" +iv_1.getWidth() + "H=" +iv_1.getHeight());
		tv_iv_2_info.setText("iv_2 W=" +iv_2.getWidth() + "H=" +iv_2.getHeight());
		tv_iv_3_info.setText("iv_3 W=" +iv_3.getWidth() + "H=" +iv_3.getHeight());
		tv_iv_4_info.setText("iv_4 W=" +iv_4.getWidth() + "H=" +iv_4.getHeight());
		tv_iv_5_info.setText("iv_5 W=" +iv_5.getWidth() + "H=" +iv_5.getHeight());
		
	}

	
	
}
